define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/ocation_class/index',
                    add_url: 'classes/ocation_class/add',
                    edit_url: 'classes/ocation_class/edit',
                    del_url: 'classes/ocation_class/del',
                    detail_url: 'classes/ocation_class/detail',
                    table: 'ocation_class',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sequence_number',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), visible: false},
                        {field: 'sequence_number', title: '班级序号', sortable: true, formatter: function(value, row) {
                            // 使用格式化的序号，如果没有则显示空字符串
                            if (row.formatted_sequence) {
                                return '<span class="text-primary font-weight-bold">' + row.formatted_sequence + '</span>';
                            }
                            return '';
                        }},
                        {field: 'name', title: '班级名称'},
                        {field: 'ocation_name', title: '教学点名称'},
                        {field: 'cover', title: '班级头像', formatter: Table.api.formatter.image},
                        {field: 'course_count', title: '课程数量'},
                        {field: 'create_time', title: '创建时间', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            
            // 加载教学点数据
            $.ajax({
                url: 'ocation/ocation/index',
                type: 'get',
                dataType: 'json',
                data: {
                    searchTable: 'ocation',
                    searchKey: 'name',
                    searchValue: '',
                    address_lv_octionlevel: 2, // 二级教学点
                    status: 1
                },
                success: function (data) {
                    var ocations = data.rows || [];
                    var html = '';
                    $.each(ocations, function (i, item) {
                        html += '<option value="' + item.id + '" data-name="' + item.name + '">' + item.name + '</option>';
                    });
                    $('#c-ocation_id').html(html).selectpicker('refresh').trigger('change');
                }
            });
            
            // 加载社区数据
            $.ajax({
                url: 'community/community/index',
                type: 'get',
                dataType: 'json',
                data: {
                    searchTable: 'community',
                    searchKey: 'name',
                    searchValue: '',
                    status: 1
                },
                success: function (data) {
                    var communities = data.rows || [];
                    var html = '';
                    $.each(communities, function (i, item) {
                        html += '<option value="' + item.id + '">' + item.name + '</option>';
                    });
                    $('#c-community_id').html(html).selectpicker('refresh');
                }
            });
        },
        edit: function () {
            Controller.api.bindevent();
            
            // 加载教学点数据
            $.ajax({
                url: 'ocation/ocation/index',
                type: 'get',
                dataType: 'json',
                data: {
                    searchTable: 'ocation',
                    searchKey: 'name',
                    searchValue: '',
                    address_lv_octionlevel: 2, // 二级教学点
                    status: 1
                },
                success: function (data) {
                    var ocations = data.rows || [];
                    var html = '';
                    var selectedId = $('#c-ocation_id').data('value') || $('#c-ocation_id').val();
                    $.each(ocations, function (i, item) {
                        html += '<option value="' + item.id + '" data-name="' + item.name + '"' + (item.id == selectedId ? ' selected' : '') + '>' + item.name + '</option>';
                    });
                    $('#c-ocation_id').html(html).selectpicker('refresh');
                }
            });
            
            // 加载社区数据
            $.ajax({
                url: 'community/community/index',
                type: 'get',
                dataType: 'json',
                data: {
                    searchTable: 'community',
                    searchKey: 'name',
                    searchValue: '',
                    status: 1
                },
                success: function (data) {
                    var communities = data.rows || [];
                    var html = '';
                    var selectedId = $('#c-community_id').data('value') || $('#c-community_id').val();
                    $.each(communities, function (i, item) {
                        html += '<option value="' + item.id + '"' + (item.id == selectedId ? ' selected' : '') + '>' + item.name + '</option>';
                    });
                    $('#c-community_id').html(html).selectpicker('refresh');
                }
            });
        },
        detail: function () {
            // 详情页面不需要特殊处理
        },
        addCourse: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});